/**
 * KopsoxApplicationServices
 * Copyright (C) 2010 Free Software Foundation, Inc. <http://fsf.org/>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kopsox.services.message;

/**
 * Standard implementation of SimpleMessage
 * 
 * @author Konrad Renner
 *
 */
public class StandardMessage implements SimpleMessage {

	private final Object value;
	private final String text;
	private final Object sender;
	
	private final int hashCode;
	
	public StandardMessage(String text,Object sender) {
		this(text,null,sender);
	}
	
	public StandardMessage(Object value,Object sender) {
		this(null,value,sender);
	}

	public StandardMessage(String text,Object value,Object sender) {
		this.value = value;
		this.text = text;
		this.sender = sender;
		
		this.hashCode = generateHashCode();
	}
	
	private final int generateHashCode() {
		final int prime = 31;
		int result = 17;
		result = prime * result + ((sender == null) ? 0 : sender.hashCode());
		result = prime * result + ((text == null) ? 0 : text.hashCode());
		result = prime * result + ((value == null) ? 0 : value.hashCode());
		
		return result;
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("StandardMessage[sender=");
		sb.append(sender);
		sb.append(",text=");
		sb.append(text);
		sb.append(",value=");
		sb.append(value);
		sb.append(']');
		
		return sb.toString();
	}
	

	@Override
	public int hashCode() {
		return this.hashCode;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		StandardMessage other = (StandardMessage) obj;
		if (sender == null) {
			if (other.sender != null)
				return false;
		} else if (!sender.equals(other.sender))
			return false;
		if (text == null) {
			if (other.text != null)
				return false;
		} else if (!text.equals(other.text))
			return false;
		if (value == null) {
			if (other.value != null)
				return false;
		} else if (!value.equals(other.value))
			return false;
		return true;
	}

	@Override
	public Object getSender() {
		return this.sender;
	}

	@Override
	public String getText() {
		return this.text;
	}

	@Override
	public Object getValue() {
		return this.value;
	}

}
